<?php

namespace app\index\controller;

use think\Cache;
use think\Controller;
use \think\Request;

//允许跨域
header("Access-Control-Allow-Origin:*");
header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept, Authorization");

class Base extends controller
{
    protected $pageId, $pageSize;
    protected $redata, $token, $uid;
    protected $filter = ['login', 'getCommentList', 'test', 'test1', 'checkcaptcha', 'sendcaptcha', 'wxpayreturn']; //不验证方法

    public function _initialize()
    {
        $this->pageId = input('pageId') ? input('pageId') : 1;
        $this->pageSize = input('pageSize') ? input('pageSize') : 10;

        $this->redata = ['content' => '', "status" => 0, 'msg' => ''];

        $request = request();
        $action = $request->action();
        $rs = in_array($action, $this->filter);
        if($rs){
            return;
        }
        // http option 是探测方法，不检测
        if($request->method() == 'OPTIONS'){
            $this->suc('1');
        }
//        //token 验证
//        $token = $request->header('token');
//        $cid = $request->header('cid');
//        // dump(S('c' . $cid));exit;
//        if(!$token || S('u' . $cid) != $token){
//            header("HTTP/1.1 401 Unauthorized");
//            echo "401";
//            exit;
//        }
    }

    /**
     * 统一输出json格式 不采用tp5自带return;
     */
    protected function echojson()
    {
        header('Content-Type: application/json;charset=utf-8');
        if ($this->redata['status']) {
            exit(json_encode($this->redata));
        }
        if ($this->redata['msg']) {
            $this->redata['status'] = 0;
        } else {
            $this->redata['status'] = 1;
        }
        exit(json_encode($this->redata));
    }

    /**
     * 快捷方法 返回出错信息
     */
    protected function err($msg)
    {
        $this->redata['msg'] = $msg;
        $this->echojson();
    }

    /**
     * 快捷方法 返回成功
     */
    protected function suc($data = '')
    {
        $this->redata['content'] = $data;
        $this->echojson();
    }

}
